﻿using System;
using System.Collections.Generic;
using System.Linq;


namespace ExcelMapper
{
    public partial class ExcelQuery
    {
        public string BasicQuery(string documentName, string columnName, string PrimaryKeyValue)
        {

            if (ExcelValidation.ValidateDocument(this.ExcelDocumentList, documentName, columnName, PrimaryKeyValue))
            {
                ExcelDocument excelDocument = this.ExcelDocumentList.Where(x => x.Name.ToLower() == documentName.ToLower()).FirstOrDefault();
                string value = string.Empty;
                var values = excelDocument.GetDocumentValues();
                var rowExcelData = values.Select(i => i).Cast<Dictionary<string, object>>().Where(d => d.ContainsKey(excelDocument.PrimaryKeyColumnName)).Where(dd => dd.ContainsValue(PrimaryKeyValue.ToString()));
                foreach (var item in rowExcelData)
                {
                    value = (string)item.Where(x => x.Key == columnName).Select(t => t.Value).FirstOrDefault();
                }
                return value;
            }
            else
            {
                throw new Exception("No exist any document called " + documentName);
            }
        }

        public T BasicQuery<T>(string documentName, string columnName, string PrimaryKeyValue)
        {

            if (ExcelValidation.ValidateDocument(this.ExcelDocumentList, documentName, columnName, PrimaryKeyValue))
            {
                var excelDocument = this.ExcelDocumentList.Where(x => x.Name.ToLower() == documentName.ToLower()).FirstOrDefault();
                string value = string.Empty;
                var values = excelDocument.GetDocumentValues();
                var rowExcelData = values.Select(i => i).Cast<Dictionary<string, object>>().Where(d => d.ContainsKey(excelDocument.PrimaryKeyColumnName)).Where(dd => dd.ContainsValue(PrimaryKeyValue));
                return ExcelFormatValue.GetData<T>(rowExcelData.FirstOrDefault(), columnName);
                
            }
            else
            {
                throw new Exception("No exist any document called " + documentName);
            }
        }

        public List<T> BasicQueryList<T>(string documentName, string columnName, string PrimaryKeyValue)
        {            
            List<T> query = new List<T>();

            if (ExcelValidation.ValidateDocument(this.ExcelDocumentList, documentName, columnName, PrimaryKeyValue))
            {
                var excelDocument = this.ExcelDocumentList.Where(x => x.Name.ToLower() == documentName.ToLower()).FirstOrDefault();
                string value = string.Empty;
                var values = excelDocument.GetDocumentValues();
                var rowExcelData = values.Select(i => i).Cast<Dictionary<string, object>>().Where(d => d.ContainsKey(excelDocument.PrimaryKeyColumnName)).Where(dd => dd.ContainsValue(PrimaryKeyValue));
                foreach (var item in rowExcelData)
                {
                   query.Add(ExcelFormatValue.GetData<T>(item, columnName));
                }
                return query;

            }
            else
            {
                throw new Exception("No exist any document called " + documentName);
            }
        }
    }
}
